from bs4 import BeautifulSoup

# 假设您的HTML文件路径为 'example.html'
file_path = r"C:\Users\l00485378\Desktop\笔试2.html"

# 读取HTML文件内容
with open(file_path, 'r', encoding='utf-8') as file:
    html_content = file.read()

# 使用BeautifulSoup解析HTML

soup = BeautifulSoup(html_content, 'html.parser')

# 查找并提取所有field ui-field-contain内容
questions = soup.find_all('div', class_='field ui-field-contain')

# 遍历每个问题
for question in questions:
    # 提取topichtml内容
    topichtml_div = question.find('div', class_='topichtml')
    topichtml_content = topichtml_div.get_text(strip=True) if topichtml_div else "No topichtml content"
    
    # 提取label内容
    labels = question.find_all('div', class_='label')
    label_contents = [label.get_text(strip=True) for label in labels] if labels else "No label content"
    
    # 打印结果
    print("问题:", topichtml_content)
    print("选项:")
    for label_content in label_contents:
        print("  ", label_content)
    print("-" * 40)  # 打印分隔线